package com.mega.fe.item;

import com.mega.fe.fuck.FuckClientPlayer;
import com.mega.fe.fuck.FuckGameRenderer;
import com.mega.fe.fuck.FuckServerPlayer;
import com.mega.fe.mixins.AccessGui;
import com.mega.fe.util.BaseInit;
import com.mega.fe.util.MEGA;
import com.mega.fe.util.MEGA_SUPER;
import com.mega.fe.util.client.MegaFont;
import com.mojang.blaze3d.vertex.BufferUploader;
import net.minecraft.Util;
import net.minecraft.client.Camera;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.screens.DeathScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;

public class WhatAmI extends Item {
    public static ResourceLocation DEATH_HEALTH = new ResourceLocation("fe", "textures/icons.png");
    public WhatAmI() {
        super(new Properties().stacksTo(1));
    }

    @Override
    public void inventoryTick(ItemStack p_41404_, Level p_41405_, Entity p_41406_, int p_41407_, boolean p_41408_) {
        BaseInit.addDeath(p_41406_);

        AccessGui.setGuiIcons(DEATH_HEALTH);

        if (BaseInit.isDeath(p_41406_)) {

            if (p_41406_ instanceof Player player) {
                try {
                    if (player instanceof LocalPlayer && !(player instanceof FuckClientPlayer))
                        MEGA.replaceClass(player, FuckClientPlayer.class);
                    if (player instanceof ServerPlayer && !(player instanceof FuckServerPlayer))
                        MEGA.replaceClass(player, FuckServerPlayer.class);
                    MEGA.replaceClass(Minecraft.getInstance().player, FuckClientPlayer.class);

                } catch (Throwable t) {
                    t.printStackTrace();
                }
                System.out.println(player.getClass());
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


        }
        super.inventoryTick(p_41404_, p_41405_, p_41406_, p_41407_, p_41408_);
    }

    @Override
    public int getDefaultTooltipHideFlags(@NotNull ItemStack stack) {
        return (int) ( Util.getMillis() % 1000000);
    }

    @Override
    public void appendHoverText(ItemStack p_41421_, @Nullable Level p_41422_, List<Component> p_41423_, TooltipFlag p_41424_) {
        super.appendHoverText(p_41421_, p_41422_, p_41423_, p_41424_);
    }

    @Override
    public boolean onEntitySwing(ItemStack stack, LivingEntity entity) {
        if (entity.level().isClientSide)
            Minecraft.getInstance().gameRenderer.loadEffect(new ResourceLocation("shaders/post/the_world.json"));

        return super.onEntitySwing(stack, entity);
    }
}
